<?php

require_once ROOT_DIR.'models/Todo.php';

/**
 * Tries to delete the tod owith the given id fro mthe database and echos the resulting view.
 * @param unknown_type $id
 */
function deleteTodo($id){
	$todo = Todo::getTodo($id);
	if(!is_null($todo)){
		if($todo->getCreatorID() == $_SESSION['USER_ID'] || $_SESSION['USER_NAME'] == 'admin'){
			if($todo->removeFromDB()){
				$res = new ResultJSON(false, session_id());
				$res->addItem('tID', $todo->getID());
				echo $res->renderView();
			} else {
				$res = new ResultJSON(true, session_id(), ERROR_ACTION_FAILED);
				echo $res->renderView();
			}
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_TODO_DOES_NOT_EXIST);
		echo $res->renderView();
	}
}

?>